package com.congapp.hot.menu;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.res.TypedArray;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.congapp.R;
import com.congapp.util.ObtenerPosicion;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;

public class Lugares extends Fragment {

    private GoogleMap googleMap;
    private MarkerOptions miUbicacion;

    private static View view;

    private Activity actividadPadre;
    private ListView mDrawerList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        actividadPadre = getActivity();
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.fragment_lugares, container, false);
        } catch (InflateException e) {
        /* map is already there, just return view as it is */
        }
        initilizeMap();

        mDrawerList = (ListView) view.findViewById(R.id.lista_lugares);

        TypedArray navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

        ArrayList<ItemLugar> ListaItemLugar = new ArrayList<ItemLugar>();

        int aleatorio = generarAleatorio(20);
        for (int i = 1; i < aleatorio; i++) {
            ListaItemLugar.add(new ItemLugar(navMenuIcons.getResourceId(0, -1), "Lugar " + i, "Carrera " + generarAleatorio(5) * i + " calle " + generarAleatorio(15) * i, "Calificacion" + generarAleatorio(5)));
        }

        AdapterListaLugar adapter = new AdapterListaLugar(actividadPadre.getApplicationContext(), ListaItemLugar);
        mDrawerList.setAdapter(adapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                displayView(position);
            }
        });

        return view;
    }

    private int generarAleatorio(int rango){
        return (int)(Math.random()*rango);
    }

    private void initilizeMap() {
        MapFragment mapFrag = (MapFragment) getFragmentManager().findFragmentById(R.id.mapa);
        googleMap = mapFrag.getMap();
        if (googleMap != null) {
            googleMap.clear();
            Location location = ObtenerPosicion.getInstance().getLocation((LocationManager) actividadPadre.getSystemService(Context.LOCATION_SERVICE));
            if (location != null) {
                LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
                if (miUbicacion == null) {
                    miUbicacion = new MarkerOptions().position(latlng).title("Aquí estás!");
                    googleMap.addMarker(miUbicacion);
                    googleMap.setMyLocationEnabled(false);
                    googleMap.getUiSettings().setCompassEnabled(false);
                    googleMap.getUiSettings().setRotateGesturesEnabled(false);
                }
                miUbicacion.position(latlng);
                CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(17).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new Lugares();
        switch (position) {
            case 0:
                fragment = new Lugares();
                break;
            default:
                break;
        }

        if (fragment != null) {
            getFragmentManager().beginTransaction().replace(R.id.frame_container, fragment).commit();
            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
}
